package com.itheima.jobs;

import com.itheima.constant.RedisConstant;
import com.itheima.utils.QiNiuUtil;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.Set;

public class ClearImgJob {
    @Autowired
    private JedisPool jedisPool;
    public void clearImg(){
        Jedis jedis = jedisPool.getResource();
        try {
            Set<String> set = jedis.sdiff(RedisConstant.SETMEAL_PIC_RESOURCES,
                    RedisConstant.SETMEAL_PIC_DB_RESOURCES);
            if(set != null){
                for (String picName : set) {
                    //删除七牛云服务器上的图片
                    QiNiuUtil.delete(picName);
                    //从Redis集合中删除图片名称
                    jedisPool.getResource().
                            srem(RedisConstant.SETMEAL_PIC_RESOURCES,picName);
                    System.out.println("----------删除七牛云图片----------");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            jedis.close();
        }
    }
}
